const app = getApp()
// pages/admin-purchase-list/admin-purchase-list.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    aduit: false,
    chenck: false,
    isQuote: false,
    activeNames: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    activeNames2: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    aTotal: 0,
    canteensList:{}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const record = JSON.parse(options.data)
    const {
      stockOrderId = 0, auxiliaryMaterialsId = 0, smallCanteenPurchaseId = 0, collarId = 0, aduit = false, chenck = false, isQuote = false, type = 0
    } = record
  
      this.setData({
        isQuote:type===3?false: isQuote,
      })
    this.setData({
      stockOrderId,
      auxiliaryMaterialsId,
      smallCanteenPurchaseId,
      collarId,
      aduit,
      chenck,
      type
    })
    if (type === 0) {
      wx.setNavigationBarTitle({
        title: '食材进货清单',
      })
      app.reqFetch.getOrderDetails({
        stockOrderId
      }, (res) => {
        this.setDetails(res)
        this.setData({ stockorder: res.stockorder})
        console.log(res.stockorder)
      })
    } else if (type === 1 ) {
      wx.setNavigationBarTitle({
        title: '主食进货清单',
      })
      app.reqFetch.auxiliarymaterialsmxDetails({
        auxiliaryMaterialsId
      }, (res) => {
        this.setDetails(res)
      })
    }else if (type === 3) {
      wx.setNavigationBarTitle({
        title: '主食领取清单',
      })
      app.reqFetch.collarmxdetails({
        collarId
      }, (res) => {
        const { collarMx } = res
        const list = [{ gys: '主食领取列表', mxlist: collarMx }]
        this.setData({
          list,
        })
      })
    }else if (type === 2){
      wx.setNavigationBarTitle({
        title: '食堂申请清单',
      })
      app.reqFetch.smallcanteenpurchasemxdetails({
        smallCanteenPurchaseId
      }, (res) => {
        this.setDetails(res)
      })
    }
  },
  setDetails(res) {
    const { list, smallCanteenPurchase} = res
    if(this.data.type===2){
      const canteensList = JSON.parse(res.smallCanteenPurchase.boxId).list
      this.setData({
        canteensList
      })
    }
   
    list.map(item => {
      var p = 0
      item.mxlist.map(ele => {
        p += ele.unitPrice * ele.purchaseNum
      })
      item.pTotal = p
    })
    let aTotal = 0
    list.map(item => {
      aTotal += item.pTotal
    })
    this.setData({
      list,
      aTotal
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  changeCollapse(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  changeCollapse2(event) {
    this.setData({
      activeNames2: event.detail
    });
  },
  handelConfirm() {
    const {
      stockOrderId,
      auxiliaryMaterialsId,
      smallCanteenPurchaseId,
      collarId,
      type
    } = this.data
    const canteenAdminId = app.globalData.userId
    
    if (type == 0) {
      app.reqFetch.pageAdminUpdate({
        stockOrderId,
        canteenAdminId,
        status: 1
      }, (res) => {
        wx.showToast({
          title: '食材审核成功',
          icon: 'none'
        })
        setTimeout(function() {
          wx.navigateBack({
            delta: 1,
          })
        }, 700)
      })
    } else if (type == 1) {
      app.reqFetch.pageAdminUpdate2({
        auxiliaryMaterialsId,
        canteenAdminId,
        status: 1
      }, (res) => {
        wx.showToast({
          title: '主食审核成功',
          icon: 'none'
        })
        setTimeout(function() {
          wx.navigateBack({
            delta: 1,
          })
        }, 700)
      })
    } else if (type == 2){
      app.reqFetch.smallcanteenpurchasepageAdminUpdate({
        smallCanteenPurchaseId,
        canteenAdminId,
        status: 1
      }, (res) => {
        wx.showToast({
          title: '小食堂审核成功',
          icon: 'none'
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 700)
      })

    }else  if(type===3) {
      app.reqFetch.collarpageAdminUpdate({
        collarId,
        canteenAdminId,
        status: 1
      }, (res) => {
        wx.showToast({
          title: '主食领取审核成功',
          icon: 'none'
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 700)
      })
    }
  },
  handleCancel(){
    wx.navigateBack({
      delta: 1,
    })
  }
})